﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-03-19 13:58:26 +0100 (Thu, 19 Mar 2009) $
// Last Changed Revision $Rev: 42 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows.Media;
using Tum.CollabXT.TFS.Properties;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System;


namespace Tum.CollabXT.TFS
{
    /// <summary>
    /// Interaction logic for TFSUploadsPage.xaml
    /// </summary>
    public partial class TFSUploadsPage : TFSPage
    {
        TFSToolProvider ToolProvider;
        IConversionWorkflow ConversionWorkflow;


        /// <summary>
        /// Constructor.
        /// </summary>
        public TFSUploadsPage()
        {
            InitializeComponent();

            UploadProcessGuidance.DisplayMode = UploadType.ProcessGuidance;
            UploadProcessGuidance.Header = Tum.CollabXT.TFS.Resources.Language.ConfigPage_ProcessGuidance;
        }

        /// <summary>
        /// Handler used to pass the provider and workflow handles to this page.
        /// </summary>
        internal override void ReceiveHandles(TFSToolProvider toolProvider, IConversionWorkflow conversionWorkflow)
        {
            ToolProvider = toolProvider;
            ConversionWorkflow = conversionWorkflow;

            SetValues();
        }

        /// <summary>
        /// Handler used when the user wants to change the current workflow page.
        /// </summary>
        internal override bool ChangePage(bool advance)
        {
            //Write libraries to tool provider
            ToolProvider.DocumentLibraries.Clear(); //Prevent double entries
            foreach (var curLibraryChild in StackPanelLibraries.Children)
            {
                var curLibrary = curLibraryChild as WssUploadItemData;
                if (curLibraryChild == null)
                    continue;

                switch (curLibrary.DisplayMode)
                {
                    case UploadType.ProcessGuidance:
                        ToolProvider.ProcessGuidancePath = curLibrary.LibraryDataPath;
                        ToolProvider.ProcessGuidanceDefaultPage = curLibrary.DefaultLibraryPage;
                        break;

                    case UploadType.AdditionalLibrary:
                        ToolProvider.DocumentLibraries.Add(new KeyValuePair<string, string>(curLibrary.LibraryName, curLibrary.LibraryDataPath));
                        break;
                }
            }

            //Page change is always allowed
            return true;
        }

        /// <summary>
        /// Updates the user interface to show all libraries present in the tool provider object.
        /// </summary>
        private void SetValues()
        {
            //Process guidance
            if (ToolProvider.ProcessGuidancePath != null)
            {
                UploadProcessGuidance.LibraryDataPath = ToolProvider.ProcessGuidancePath;

                if (ToolProvider.ProcessGuidanceDefaultPage != null)
                {
                    try
                    {
                        UploadProcessGuidance.DefaultLibraryPage = ToolProvider.ProcessGuidancePath + "/" + ToolProvider.ProcessGuidanceDefaultPage;
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
            }

            //Additional libraries
            foreach (KeyValuePair<string, string> curAdditionalLibrary in ToolProvider.DocumentLibraries)
            {
                var newLibraryUpload = CreateUploadItemData();
                newLibraryUpload.LibraryName = curAdditionalLibrary.Key;

                if(curAdditionalLibrary.Value != null)
                    newLibraryUpload.LibraryDataPath = curAdditionalLibrary.Value;

                StackPanelLibraries.Children.Add(newLibraryUpload);
            }
        }

        /// <summary>
        /// Handler for the click on the 'add library' button.
        /// </summary>
        private void ButtonAddLibrary_Click(object sender, RoutedEventArgs e)
        {
            //Create library item
            var newLibraryUpload = CreateUploadItemData();
            StackPanelLibraries.Children.Add(newLibraryUpload);

            //Force new processing
            ConversionWorkflow.InvalidateProcessing();
        }

        /// <summary>
        /// Creates a new and initialized instance of WssUploadItemData.
        /// </summary>
        private WssUploadItemData CreateUploadItemData()
        {
            var newItem = new WssUploadItemData()
            {
                Header = Tum.CollabXT.TFS.Resources.Language.ConfigPage_AdditionalLibrary,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5),
            };
            newItem.Remove += new System.EventHandler(AdditionalLibraryControl_Remove);
            newItem.Modified += new EventHandler(AdditionalLibraryControl_Modified);
            return newItem;
        }

        /// <summary>
        /// Event handler that invalidates the current processing in case data of a library control is changed.
        /// </summary>
        void AdditionalLibraryControl_Modified(object sender, EventArgs e)
        {
            ConversionWorkflow.InvalidateProcessing();
        }

        /// <summary>
        /// Event handler that removes a given library control from the user interface
        /// </summary>
        void AdditionalLibraryControl_Remove(object sender, System.EventArgs e)
        {
            //Remove library control
            var senderElement = (UIElement)sender;
            if (StackPanelLibraries.Children.Contains(senderElement))
                StackPanelLibraries.Children.Remove(senderElement);

            //Force new processing
            ConversionWorkflow.InvalidateProcessing();
        }
    }
}
